<?php
namespace jy_weishop_en;
defined('BY_JYA') or exit('error');
/*
	商品详情页
*/
class goods_item extends \plugin{
	/*
		商品详情页
	*/
	public function get_goodsitem($return=array()){
		global $_RQ;
		$id=intval($_RQ['id']);
		//来自其他插件的购物
		$this->session('fromplugin_name',$_RQ['plugin_name']);
		$this->session('frompid',intval($_RQ['frompid']));
		$this->session('active_plugin',$_RQ['active_plugin']);
		$store_id=$_SESSION['store_id'];
		if($id){
			$_SESSION['goodsid'] = $id;
		}
		if(!$id){
			exi('没有商品ID','error','/index/');
		}
		$goods=pdo_get('jy_weishop_goods',array('id'=>$id,'status'=>1,'uniacid'=>$_SESSION['uniacid']));
		if(!$goods){
			exi('选择的商品不存在或已下架，请重新选择！','error','/index/');
		}
		$goods['originalprice']=$goods['originalprice']>1?$goods['originalprice']:price($goods['price']*1.6);
		$goods['userbuy'] = 1;
		$goods['levelbuy'] = 1;
		$goods['groupbuy'] = 1;
		$goods['image']=$goods['image']?tomedia($goods['image']):'';
		$goods['video']=$goods['video']?tomedia($goods['video']):'';
		$goods['images']=json_decode($goods['images'],true);
		foreach($goods['images'] as &$image){
			$image=tomedia($image);
		}
		array_unshift($goods['images'],tomedia($goods['image']));
		$goods['pcatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$goods['pcate']),'title');
		$goods['ccatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$goods['ccate']),'title');
		$return['cate']=array($goods['pcatetitle'],$goods['ccatetitle']);
		//来自分销MID
		$goods['myshopmid']=intval($_RQ['frommyshop']);
		$jy_commission = $this->plugin_exists('jy_commission');
		if($jy_commission && $goods['myshopmid']){
			$member=pdo_get('core_members',array('id'=>$goods['myshopmid']),array('id','avatar','nickname'));
			$goods['myshop']=pdo_get('jy_commission_shop',array('mid'=>$goods['myshopmid'],'uniacid'=>$_SESSION['uniacid']));
			$goods['myshop']['logo']=$goods['myshop']['logo']?tomedia($goods['myshop']['logo']):tomedia($member['avatar']);
			$goods['myshop']['title']=$goods['myshop']['title']?$goods['myshop']['title']:$member['nickname'].'的小店';
		}
		//来自海报
		$jy_poster = $this->plugin_exists('jy_poster');
		if($jy_poster  && $return['setting']['no_poster']!=1){
			$goods['isposter']=1;
		}
		//来自多商户店(非下级也可以分销）
		$goods['merchid']=$_RQ['merchid']?intval($_RQ['merchid']):$goods['merchid'];
		$this->session('frommerchid',$goods['merchid']);
		$jy_merch = $this->plugin_exists('jy_merch');
		if($jy_merch && $goods['merchid']){
			$goods['merch']=pdo_get('jy_merch_user',array('id'=>$goods['merchid'],'uniacid'=>$_SESSION['uniacid']));
			if($goods['merch']['status']!=1){
				exi('该商品的商户还未上线！','error','/index/');
			}
			$member=pdo_get('core_members',array('id'=>$goods['merch']['mid']),array('id','avatar','nickname'));
			$goods['merch']['logo']=$goods['merch']['logo']?tomedia($goods['merch']['logo']):tomedia($member['avatar']);
			$goods['merch']['merchname']=$goods['merch']['merchname']?$goods['merch']['merchname']:$member['nickname'].'的小店';
			$jy_kefu = $this->plugin_exists('jy_kefu');
			if($jy_kefu && $goods['merch']['iskefu']){
				$goods['merch']['showkefu']=1;
			}
			if(!$goods['merch']['id']){
				$goods['merch']['merchname']='该商户不存在';
			}
		}
		pdo_update('jy_weishop_goods',array('readnum +='=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		//浏览权限
		$levelinfo = $this->model('member')->get_level();
		if($goods['showlevels']){
			$fastvalue=substr($goods['showlevels'],0,1);
			if($fastvalue!=';'){
				$goods['showlevels']=';'.$goods['showlevels'];
			}
			$lastvalue=substr($goods['showlevels'],-1,1);
			if($lastvalue!=';'){
				$goods['showlevels']=$goods['showlevels'].';';
			}
			if(strpos($goods['showlevels'],';'.$levelinfo['id'].';')===false){
				exi('您没有权限浏览该商品！','error',$return['shopset']['memberopen']?:'jy_weishop_en/member.open/');
			}
		}	
		$groupinfo = $this->model('member')->get_group();
		if($goods['showgroups']){
			$fastvalue=substr($goods['showgroups'],0,1);
			if($fastvalue!=';'){
				$goods['showgroups']=';'.$goods['showgroups'];
			}
			$lastvalue=substr($goods['showgroups'],-1,1);
			if($lastvalue!=';'){
				$goods['showgroups']=$goods['showgroups'].';';
			}
			if(strpos($goods['showgroups'],';'.$groupinfo['id'].';')===false){
				exi('您没有权限浏览该商品！','error',$return['shopset']['memberopen']?:'jy_weishop_en/member.open/');
			}
		}
		//更新足迹
		$history=pdo_getcolumn('jy_weishop_member_history',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id),'id');
		if($history){
			pdo_update('jy_weishop_member_history',array('goodsid'=>$id,'createtime'=>time()),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id));
		}else{
			pdo_insert('jy_weishop_member_history',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id,'createtime'=>time()));
		}
		//判断是否收藏：
		$return['islike']=pdo_getcolumn('jy_weishop_member_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id),'id');
		//判断是否收藏：
		$return['isaddqingdan']=pdo_count('jy_weishop_member_qingdan',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id),'id');
		//判断买家秀是否开启
		//买家秀
		$buyxiu=$this->plugin_setting('buyxiu');
		if($buyxiu['open']){
			foreach($buyxiu['goods_ids'] as $xiuv){
				if($xiuv['id'] == $goods['id']){
					$goods['isbuyxiu']=1;
				}
			}
		}
		if($goods['isbuyxiu'] && $buyxiu['likes']){
			$install = $this->plugin_exists('jy_vote');
			if(!$install){
				$goods['isbuyxiu'] =0;
			}
		}
		$install = $this->plugin_exists('jy_commission');
		if($install){
			$goods['jy_commission'] =1;
		}
		//营销设置
		$saleset=$this->plugin_setting('saleset');
		$member=pdo_get('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		$member2=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),array('avatar','nickname','credit1','credit2'));
		$member=array_merge($member,$member2);
		$goods['canbuy'] = ($goods['status'] == 1) && empty($goods['deleted']) && ($goods['total'] > 0);
		$goods['unit'] = ((empty($goods['unit']) ? '件' : $goods['unit']));
		$goods['subtitle'] = cfc('tools')->substr($goods['subtitle'],0,218);
		if($goods['weight']){
			$goods['weight']=round($goods['weight']/1000,2);
		}
		if (0 < $goods['usermaxbuy']) {
			$order_goodscount = pdo_getsum('jy_weishop_order_goods',array('ispay'=>1,'goodsid' => $goods['id'],'uniacid' => $_SESSION['uniacid'],'mid' => $_SESSION['uid']),'total');
			$goods['order_goodscount']=$order_goodscount;
			if ($goods['usermaxbuy'] <= $order_goodscount) {
				$goods['userbuy'] = 0;
				$goods['canbuy'] = false;
			}
		}
		$goods['fullbackfee']=$this->model('activity')->get_fullback($goods);
		if ((0 < $goods['isendtime']) && (0 < $goods['endtime']) && ($goods['endtime'] < time())) {
			$goods['canbuy'] = false;
		}
		//配送设置
		$getdispatch=$this->model('goods')->get_dispatch($goods['id']);
		$goods=array_merge($goods,$getdispatch);
		//默认已选参数：
		$pindex = max(1, 1);
		$parameter=array(
						'params'=>array('goodsid'=>$id),
						'limit'=>array($pindex,1),
						'fields'=>$fields,
						'orderby'=>'price asc',
		);
		$api_data=json_decode($goods['api_data'],true);
		if($api_data['istaocan']==1){
			$parameter['params']['specs']='';
			$specs_list=pdo_getall('jy_weishop_goods_option',array('goodsid'=>$id,'specs'=>''));
			$optionsid=$specs_list[0]['id'];
			$selespename = $specs_list[0]['title'];
			$goods['istaocan'] = 1;
		}else{
			$optionsone = pdo_getlist('jy_weishop_goods_option',$parameter);
		}
		if($optionsone){
			$optionsid=$optionsone[0]['id'];
			if($api_data['istaocan']==1){
			}else{
				$spsids=$optionsone[0]['specs'];
				//规格
				$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
				//已选规格
				$selectspecs = explode('_',$spsids);
				$selespename = '';
				if($specs_list){
						foreach($selectspecs as $spv){
							$spsitem=pdo_get('jy_weishop_goods_spec_item',array('id'=>$spv,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
							if($spsitem['title']){
								$selespename .=$spsitem['title'].'，';
							}
						}
						foreach($specs_list as $k=>&$sp){
							$sp['items']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sp['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
						}
						if(count($specs_list)>0){
							$goods['optiontotal']=pdo_getsum('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$id),'stock');
							if($goods['optiontotal']<0){
								$goods['canbuy']==false;
							}else{
								$goods['canbuy']==true;
							}
						}
				}
			}
		}
		//默认商品规格价格：
		$goods['optionid']=$optionsid;
		$goods['storeid']=$store_id;
		$goods=$this->model('activity')->get_upgoods($goods);
		//二次更新库存后
		if($goods['canbuy'] && $goods['total']==0){
			$goods['canbuy'] = false;
		}
		$return['options']=$goods['options']?:array();
		$return['options']['thumb']=$return['options']['thumb']?tomedia($return['options']['thumb']):tomedia($goods['image']);
		$goods['image']=$return['options']['thumb']?:$goods['image'];
		$return['selespename'] = $selespename;
		$option_list=pdo_getall('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$id),array('id','specs','thumb','stock','price','productprice','costprice','goodssn','productsn','weight'),'','paixu asc');
		if($option_list){
				foreach($option_list as &$i){
					$i['specs']=explode('_',$i['specs']);
					foreach($i['specs'] as $j){
						$op=pdo_get('jy_weishop_goods_spec_item',array('id'=>intval($j)),array('specid','title'));
						$spec_title=pdo_getcolumn('jy_weishop_goods_spec',array('id'=>$op['specid']),'title');
						$i[$spec_title]=$op['title'];
					}
					$i['num']=$i['stock'];
					$i['old_price']=$i['productprice'];
					$i['cost_price']=$i['costprice'];
					$i['bm']=$i['goodssn'];
					$i['tm']=$i['productsn'];
					$i['image']=$i['thumb'];
					//规格图片
					if($i['thumb']){
						$goods['images'][]=tomedia($i['thumb']);
					}
				}
				//$specs_list['option']=$option_list;
		}
		$goods['images']=array_unique($goods['images']);
		$return['specs_list'] = $specs_list;
		
		//扩展属性
		if($goods['labelid']){
			$goods['labelname'] = pdo_getcolumn('jy_weishop_goods_label',array('id'=>$goods['labelid'],'uniacid'=>$_SESSION['uniacid']),'labelname');
			$goods['labelname'] = json_decode($goods['labelname'],true);
		}
		/*
			赠品
			先根据订单金额找赠品，再根据商品找赠品
		*/
		$gifts=$this->model('activity')->get_goods_gifts($goods);
		$goods['gift_id']=$gifts?$gifts[0]['gift_id']:'';
		//评价
		$comment_total=0;
		$get_commentsnum=$_SESSION['os']=='windows'?10:3;
		$return['list'] = $this->model('goods')->get_comments($id,$get_commentsnum,$comment_total);
		$return['commentsnum'] = $comment_total;
		$return['commentsfen'] = $this->model('goods')->count_comments($id);
		$return['comments_type'] = 'all';
		//会员等级折扣
		$level = pdo_get('jy_weishop_member_level',array('id'=>$levelinfo['id'],'uniacid'=>$_SESSION['uniacid']));
		$level['discount'] = $level['discount']?$level['discount']:10;
		$member['memberprice'] =  $this->memberprice($goods,$level);
		$member['levelname'] =$level['title'];
		if(strpos($goods['no_de_sales'],'no_member_discount')===false && $return['setting']['no_vipshow']!=1){
			$member['isdiscount']=1;
		}
		$return['address_list'] = pdo_getall('core_address',array('isdel'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if($return['address_list']){
			foreach($return['address_list'] as &$add){
				$add['pcd']=json_decode($add['pcd'],true);
			}
		}
		//可用优惠券
		$coupons=$this->model('coupon')->getmycoupon_ing(1,$goods['price'],$goods['level_de_price'],array($goods));
		//会员默认地址
		$address =  pdo_get('core_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'isdel'=>0,'isdefault'=>1));
		//收货地址：
		$address['pcd']=json_decode($address['pcd'],true);
		//$seleaddress = $address['pcd']['province'].$address['pcd']['city'].$address['pcd']['district'].$address['address'];
		$return['address'] = $address;
		//获取行政数据
		$return['province']=cfc('tools')->pcd();
		if(!$goods['type'] && $address['id']){
			//预计送达时间
			$return['deliverytime']=$this->model('goods')->get_deliverytime($address);
		}
		//是否可添加购物车
		$canAddCart = true;
		//$gifts 有礼品的一样可以加入购物车。
		//if ($goods['type'] == 1 || $goods['type'] == 2 || $goods['type'] == 3 || ($goods['type'] == 20) || !(empty($goods['cannotrefund']))){
		//	$canAddCart = false;
		//}
		$cartCount = $_SESSION['uid']?pdo_getsum('jy_weishop_member_cart', array('mid' => $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total'):0;
		//商户满减
		if($saleset['enough']){
			paixu($saleset['enough'],'man','desc');
		}
		//返现
		if($goods['fang_credit2']){
			if(substr($goods['fang_credit2'],-1)=='%'){
				$goods['fang_credit2']=($goods['price']*substr($goods['fang_credit2'],0,-1))/100;
			}
		}
		//送积分
		$goods['credit']=$this->model('activity')->get_buy_credit($goods);
		//送虚拟币
		$credit3_order=array(
			'price'=>$goods['price'],
			'goods'=>array(0=>$goods),
		);
		$credit3_order['goods'][0]['goodsid']=$goods['id'];
        $credit3_order['goods'][0]['total']=1;
		$goods['credit3']=$this->model('activity')->get_buy_credit3_order($credit3_order,'',true,$goods['merchid']);
		//积分抵扣
		$goods['deduct_credit']=$this->model('activity')->get_deduct_credit($goods,$saleset,$member);
		//满减、包邮活动
		if(($goods['type'] !=1 && $goods['isverify'] !=1 && $goods['dispatchtype'] ==1 && $goods['dispatchprice']==0) || $saleset['baoyou_status']==1 || $goods['fullnum']>0 || $goods['fullmoney']>0 ){
			$return['manjian']=1;
			if($saleset['baoyou_levelstatus'] && $saleset['baoyou_levelset']){
				$levelid = $member['level'];
				$levelid=$levelid?:0;
				foreach($saleset['baoyou_levelset'] as $sv){
					if($sv['level_id']==$levelid && $saleset['baoyou_man']>$sv['manfee']){
						$saleset['baoyou_man']=$sv['manfee'];
						break;
					}
				}
			}
		}
		//抵扣、赠送积分活动
		if(($goods['deduct_credit'] && $goods['deduct_credit'] !='0.00') || $goods['credit']){
			$return['deduct_credit']=1;	
		}
		if($goods['credit']>0 || count($goods['credit3']) || $saleset['enough'] || $return['manjian'] || $return['deduct_credit']){
			$return['huodong']=1;
		}else{
			$return['huodong']=0;
		}
		if(($goods['credit']>0 || count($goods['credit3']) || !empty($coupons) || $return['isdiscount'] || $return['huodong'] || $credit['buy'] || $goods['buyagain']>0)){
			$return['sale']=1;
		}else{
			$return['sale']=0;
		}
		//商品描述为空
		//去掉多余空样式
		$goods['content']=str_replace('style=""','',$goods['content']);
		$goods['content']=$goods['content']?$goods['content']:'<div></div>';
		//参数
		$goods['param']=json_decode($goods['param'],true);
		//问答
		$wenda = array();
		$return['wenda']=$wenda;
		//货物地址：
		$goods['pcd'] = json_decode($goods['pcd'],true);
		//小程序二维码：
		$url='/jy_weishop_en/goods/detail/index?id='.$_SESSION['goodsid'].'&fromuser='.$_SESSION['uid'];
		$pid='wxappnew'.$_SESSION['wxapp_id'].'_'.$_SESSION['goodsid'].'_'.$_SESSION['uid'];
		if($_SESSION['fromh5wxapp']){
			$h5url='jy_weishop_en/goods.detail//id='.$_SESSION['goodsid'].'&fromuser='.$_SESSION['uid'];
			$url='/pages/index/index?url='.urlencode($h5url);
		}
		$goods['appcode'] = $this->model('model')->get_wxappewm($url,$pid);
		$goods['subtitle1']=cfc('tools')->substr($goods['subtitle1'],0,13);
		$goods['title1']=cfc('tools')->substr($goods['title'],0,14);
		$goods['title2']=cfc('tools')->substr($goods['title'],14,13);
		$goods['title2']=$goods['subtitle1']?$goods['subtitle1']:$goods['title2'];
		$goods['title2']=$goods['title2']?$goods['title2'].'...':'暂无描述';
		//海报文件
		if($goods['isposter']){
			if($_SESSION['fromh5wxapp'] || $_SESSION['container']=='wxapp'){
				$poster=model('jy_poster/model')->get_active(3,'',$goods['id'],$_SESSION['openid'],$goods['appcode'],'','jy_weishop');
			}else{
				$poster=model('jy_poster/model')->get_active(3,'',$goods['id'],$_SESSION['openid'],'','','jy_weishop');
			}
		}
		if($goods['cant_buy']==1){
			$goods['canbuy'] = false;
		}
		//判断nobuy状态：
		if($goods['userbuy']==0){
			$goods['nobuytitle']='您已经超出最大'.$goods['usermaxbuy'].'件购买量';
		}elseif($goods['levelbuy']==0){
			$goods['nobuytitle']='您当前会员等级没有购买权限';
		}elseif($goods['groupbuy']==0){
			$goods['nobuytitle']='您所在的用户组没有购买权限';
		}elseif($goods['timebuy']==-1){
			$goods['nobuytitle']='未到开始抢购时间!';
		}elseif($goods['timebuy']==1){
			$goods['nobuytitle']='抢购时间已经结束';
		}elseif($goods['cant_buy']==1){
			$goods['nobuytitle']='不满足购买条件';
		}else{
			$goods['nobuytitle']='商品已售罄';
		}
		//POST提交：
		$post = array();
		if($_SESSION['current_plugin']=='jy_weidish' || $_SESSION['current_plugin']=='jy_weishoppei' || $_SESSION['current_plugin']=='jy_weishoppeishop' || $_SESSION['current_plugin']=='jy_weishopscale'){
			$post['isdish'] = true;
		}
		$post['num'] = 1;
		$post['goodsid'] = $id;
		$post['specid']=$selectspecs;
		$post['addressid']=$address['id']?:0;
		$post['optionsid']=$return['options']['id']?:0;
		$post['storeid']=$_SESSION['store_id'];
		$return['isvideo']=1;
		$return['post']=$post;
		$goods['url']=custom_url('jy_weishop_en/goods.detail//id='.$goods['id'],true);
		$isbuynum=pdo_count('jy_weishop_order_goods',array('ispay'=>1,'mid'=>$_SESSION['uid'],'goodsid'=>$goods['id']),'id');
		$goods['isbuynum']=$isbuynum?:0;
		//底部固定信息
		$goodsbasic=$this->plugin_setting('goodsbasic');
		$goods['footerinfo']=$goodsbasic['footerinfo'];
		if($return['shopset']['no_showsales']){
			$goods['isshowsales']=1;
		}
		if($return['shopset']['no_showtotal']){
			$goods['isshowtotal']=0;
		}
		$return['goods']=$goods;
		$return['posterimg']=$poster['poster'];
		$return['canAddCart']=$canAddCart;
		$return['cartCount']=$cartCount;
		$return['coupons']=$coupons;
		$return['gifts']=$gifts;
		$return['isgift']=$isgift;
		$return['has_city']=$goods['has_city'];
		$return['citys']=$goods['citys'];
		$return['dispatch']=$getdispatch;
		$return['spec_titles']=$spec_titles;
		$return['images']=$goods['images'];
		$return['saleset']=$saleset;
		$return['showtype']=0;
		//好物圈
		$install = $this->plugin_exists('jy_hwq');
		$return['jy_hwqset']=array();
		if($install){
			$return['jy_hwqset']=plugin('jy_hwq')->plugin_setting('basic');
		}
		$return['jy_hwqset']=$return['jy_hwqset']?:array();
		//现在时间时间戳
		$return['time']=time();
		$hasServices = $goods['cash'] || $goods['seven'] || $goods['repair'] || $goods['invoice'] || $goods['quality'];
		$return['hasServices']=$hasServices;
		$return['url']=$return['goodsurl']=custom_url('jy_weishop_en/goods.detail//id='.$id,true);
		//set_html('title',$goods['title']);
		$goods['desc']=$goods['share_desc']?:$goods['subtitle'];
		$imgUrl=$goods['share_icon']?:$goods['image'];
		$return['sharedata']=array(
			'title'=>$goods['share_title']?:$goods['title'],
			'desc'=>$goods['desc']?:$goods['title'],
			'link'=>custom_url('jy_weishop_en/goods.detail//id='.$id.'&fromuser='.$_SESSION['uid'].'&current_plugin='.$_SESSION['current_plugin'],true),
			'imgUrl'=>tomedia($imgUrl)
		);
		//如果是朋友圈商城代理，调整分享连接地址
		if($goods['active']['kfewm']){
				$return['kfewm']=$goods['active']['kfewm'];
		}
		if($goods['active']['sharedata']){
				$return['sharedata']=array_merge($return['sharedata'],$goods['active']['sharedata']);
		}
		if(empty($return['goods']['active'])){
			$return['goods']['active']=$this->model('activity')->get_sales($goods['id']);
			if($return['goods']['active']['discount']>10){
				$return['goods']['oldprice']=$return['goods']['price'];
				$return['goods']['price']=$return['goods']['oldprice']*$return['goods']['active']['discount']/100;
			}
		}
		$fans=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','mobile','avatar','nickname'),true);
		$fans['time']=date('Y-m-d H:i:s',TIMESTAMP);
		$fans['nickname']=$fans['nickname']?$fans['nickname']:'匿名';
		$member=array_merge($fans,$member);
		$return['member']=$member;
		$return['item']['price']=price($return['item']['price']);
		//小程序直播设置
		$sale_live=$this->plugin_setting('sale_live');
		if($_RQ['room_id']){//直播端roomid房间跟商品绑定
			$this->up_live_rooms($_RQ['room_id'],$_RQ['wxgid'],$sale_live);
		}
		if($sale_live['isopen']){
			$return['roomsid']=pdo_getcolumn('core_account_wxapp_live_rooms',array('goodsid'=>$goods['id'],'uniacid'=>$_SESSION['uniacid']),'roomid');
		}
		if(!$post['isdish'] && !$return['selespename'] && (!$return['seckillinfo'] ||  $return['seckillinfo']['status']!=1) && $goods['canbuy']){
			$return['gobuy']=1;
		}
		if(!$post['isdish'] && $return['selespename'] && (!$return['seckillinfo'] ||  $return['seckillinfo']['status']!=1) && $goods['canbuy']){
			$return['gobuy']=2;
		}
		if($goods['store_isopen']){
			$store=pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goods['storeid']),array('id','title','pcd','address'));
			$store['pcd']=$store['pcd']?json_decode($store['pcd'],true):array();
			$return['selectstore']=$store;
		}
		return $return;
	}
	public function up_live_rooms($room_id,$wxgid=0,$sale_live=array()){
			$room_goods=$this->session('room_goods')?:array();
			$sale_live=$sale_live?:$this->plugin_setting('sale_live');
			$wxgid=$_SESSION['wxapp_id']?:intval($wxgid);
			$live_id=pdo_getcolumn('core_account_wxapp_live',array('wxapp_id'=>$wxgid,'uniacid'=>$_SESSION['uniacid']),'id');
			//兑换本地直播ID
			$roomid=pdo_getcolumn('core_account_wxapp_live_rooms',array('roomid'=>$room_id,'live_id'=>$live_id,'uniacid'=>$_SESSION['uniacid']),'id');
			if($sale_live['getfeetype']==1){
				if(!$room_goods[$id]){//是初次绑定
					$room_goods[$id]=intval($roomid);
					$this->session('room_goods',$room_goods);
				}
			}else{
				$room_goods[$id]=intval($roomid);
				$this->session('room_goods',$room_goods);
			}
			pdo_update('core_account_wxapp_live_rooms',array('fromlook +='=>1),array('id'=>$roomid,'uniacid'=>$_SESSION['uniacid']));
	}
	public function memberprice($goods,$level=array()){
		if($goods['isdiyprice']==1){
			$goods['diyprice']=json_decode($goods['diyprice'],true);
			if($goods['diyprice']){
				$levelinfo = $levelinfo?:$this->model('member')->get_level();
				foreach($goods['diyprice'] as $dv){
					if($levelinfo['id']==$dv['levelid']){
						if($goods['optionid']){
							$minprice = pdo_get('jy_weishop_goods_option',array('goodsid'=>$goods['id']),'','price asc');
							$maxprice = pdo_get('jy_weishop_goods_option',array('goodsid'=>$goods['id']),'','price desc');
							if($dv['pricerate']>0){
								$memberprice=price($minprice['price']*$dv['pricerate']/100);
								$memberprice .='~'.price($maxprice['price']*$dv['pricerate']/100);
							}
						}else{
							$memberprice=price($dv['price'],true);
						}
						break;
					}
				}
			}
		}else{
			if(!$level['discount']){
				return $goods['price'];
			}
			$memberprice=price($goods['price'] * ($level['discount']/10));
			if($goods['optionid']){
				$maxprice = pdo_get('jy_weishop_goods_option',array('goodsid'=>$goods['id']),'','price desc');
				if($maxprice['price']>$goods['price']){
					$memberprice .='~'.price($maxprice['price']*($level['discount']/10));
				}
			}
		}
		return $memberprice;
	}
	public function up_citys($citys){
		//多个省时，就忽略
		if(count($citys)>1){
			return $citys;
		}
		if(count($citys)==1){
			foreach($citys as $k=>$v){
				//多个城市
				$quyu1=array();
				if(count($v)>1){
					foreach($v as $ck=>$cv){
						$quyu1[$ck]=true;
					}
					return $quyu1;
				}
				if(count($v)==1){
					foreach($v as $ck=>$cv){
						return $cv;
					}
				}
			}
			return $citys;
		}
	}
	public function goodslist($params=array()){
		global $_RQ;
		if(trim($_RQ['keyword'])){
			$params['title like']='%'.trim($_RQ['keyword']).'%';
			$return['params']['keyword']=trim($_RQ['keyword']);
		}
		$_RQ['pluginname']=$_RQ['pluginname']?:$params['onepluginname'];
		//有关键词时，分类ID也检索！
		if(($_RQ['pcate'] || $_RQ['pid']) && $_RQ['pid']!=-1){
				$pcate=$_RQ['pcate']?intval($_RQ['pcate']):intval($_RQ['pid']);
				$params['pcate']=$pcate;
				$return['params']['pcate']=$pcate;
		}
		if($_RQ['ccate'] || $_RQ['cid']){
				$ccate=$_RQ['ccate']?intval($_RQ['ccate']):intval($_RQ['cid']);
				$params['ccate']=$ccate;
				$return['params']['ccate']=$ccate;
		}
		if($_RQ['scate'] || $_RQ['sid']){
				$scate=$_RQ['scate']?intval($_RQ['scate']):intval($_RQ['sid']);
				$params['scate']=$scate;
				$return['params']['scate']=$scate;
		}
		if($_RQ['order']=='isrecommand'){
			$params['saletype likes']=array('1');
			$return['params']['order']='isrecommand';
		}elseif($_RQ['order']=='isnew'){
			$params['saletype likes']=array('2');
			$return['params']['order']='isnew';
		}elseif($_RQ['order']=='ishot' && !$_RQ['ccate'] && !$_RQ['pcate'] && !$_RQ['scate']){
			$params['saletype likes']=array('3');
			$return['params']['order']='ishot';
		}else{
			$return['params']['order']=$_RQ['order'];
		}
		if($_RQ['priceMin']){
			$params['price >=']=intval($_RQ['priceMin']);
			$return['params']['priceMin']=intval($_RQ['priceMin']);
		}
		if($_RQ['priceMax']){
			$params['price <=']=intval($_RQ['priceMax']);
			$return['params']['priceMax']=intval($_RQ['priceMax']);
		}
		if($_RQ['isvideogoods'] || $_RQ['order']=='isvideogoods'){
			$params['video <>']='';
			$return['params']['isvideogoods']=intval($_RQ['isvideogoods']);
		}
		if($_RQ['star']){
			$params['star >=']=intval($_RQ['star']);
			$return['params']['star']=intval($_RQ['star']);
		}
		//电商客商品
		if($_RQ['isweishopke']){
			$params['pluginname']=array('jy_taobaoke','jy_jdk','jy_pddke');
			$return['params']['isweishopke']=1;
		}elseif(!isset($params['onepluginname']) && !isset($params['onepluginname OR'])){
			$onepluginname=model('jy_weishop_en/goods')->is_onegoods($_SESSION['current_plugin'],$_RQ['pluginname']);
			if($onepluginname){//开启独立商品
					$params['onepluginname']=$onepluginname;
					$return['params']['onepluginname']=$onepluginname;
			}else{
					$params['onepluginname']='';
			}
		}
		//插件关联：
		if(trim($_RQ['pluginname'])){
			$params['pluginname']=trim($_RQ['pluginname']);
			unset($params['onepluginname']);
			$return['params']['pluginname']=trim($_RQ['pluginname']);
		}
		if($params['merchid']){
			unset($params['onepluginname']);
		}
		if(!$params['pluginname']){
			$params['pluginname !=']=array('jy_taobaoke','jy_jdk','jy_pddke');
		}
		if($return['shopset']['no_showmerch']==1){
			$params['merchid']=0;
		}
		//进一步整理筛选条件，商品属性字段是否需 要调整，商品分类如何筛选，orderby如何整理
		$pindex = max(1, intval($_RQ['page']));
		$psize = $_SESSION['os']=='windows'?12:10;
		if($_RQ['num']){
			$psize=$_RQ['num'];
		}
		$total='';
		$fields=array('id','title','merchid','status','total','subtitle','image','video','price','active_plugin','active_id','salenum','pluginname','originalprice','usermaxbuy','minbuynum','unit','api_data');
		if($_RQ['wxappjiaoyi']){
			$fields[]='api_id';
			$params['api_id >']=0;
			$params['api_pluginname']='jy_wxappjiaoyib';
		}else{
			$params['api_pluginname <>']='jy_merch';
		}
		$orderby='paixu desc, star desc,  salenum desc, id desc ';
		if($_RQ['order']){
			if($_RQ['order']=='sales'){
				$orderby='salerealnum desc, paixu desc';
			}
			if($_RQ['order']=='new'){
				$orderby='id desc, paixu desc';
			}
			if($_RQ['order']=='star'){
				$orderby='star desc, paixu desc';
			}
			if($_RQ['order']=='you'){
				$orderby='salenum desc, paixu desc';
			}
			if($_RQ['order']=='price'){
				if($_RQ['order_type']==2){
					$orderby='price desc';
				}else{
					$orderby='price asc';
				}
			}
		}
		if($_RQ['sale_type']==1){
			$orderby='salerealnum desc, paixu desc';
		}elseif($_RQ['sale_type']==2){
			$orderby='salerealnum asc, paixu desc';
		}
		if($_RQ['color']){
			$params['api_data like']='%'.trim($_RQ['color']).'"%';
		}
		if(($_RQ['order']=='upvip' || $_RQ['order']=='sale_limit' || $_RQ['order']=='sale_presell'  || $_RQ['order']=='sale_cuxiao' || $_RQ['order']=='sale_offer' || $_RQ['order']=='sale_new' || $_RQ['order']=='sale_libao') && !$return['params']['pcate'] && !$return['params']['ccate']){
			//if(intval($_RQ['page'])==1){
				if( $_RQ['order']=='sale_offer'){
					$getoffer=model('jy_weishop_en/activity/sales')->getoffer();
					$list=$getoffer['list'];
				}
				if( $_RQ['order']=='sale_cuxiao'){
					$list=model('jy_weishop_en/activity/sales')->goods_list(500);
				}
				if( $_RQ['order']=='sale_limit'){
					$list=model('jy_weishop_en/activity/limit')->goods_list(500);
				}
				if( $_RQ['order']=='sale_presell'){
					$list=model('jy_weishop_en/activity/presell')->goods_list(500);
				}
				if( $_RQ['order']=='sale_new'){
					$sale_new=model('jy_weishop_en/activity/sales')->getnew();
					$list=$sale_new['list'];
				}
				if( $_RQ['order']=='sale_libao' && $_RQ['sale_libaoid']){
					$return['params']['sale_libaoid']=$_RQ['sale_libaoid'];
					$sale_libao=model('jy_weishop_en/activity/sales')->getlibao($_RQ['sale_libaoid']);
					$list=$sale_libao['list'];
				}
				if($_RQ['order']=='upvip'){
					$nextlevel=model('jy_weishop_en/member')->get_nextlevel();
					$nextlevel['goods_ids']=json_decode($nextlevel['goods_ids'],true);
					if($nextlevel['up_type']!=2){
						if(count($nextlevel['goods_ids'])){
							$list=$nextlevel['goods_ids'];
						}else{
							$isnogoods_ids=true;
						}
					}else{
						$isnogoods_ids=true;
					}
				}
				$ids=array();
				foreach($list as &$ov){
					$ids[$ov['id']]=$ov['id'];
				}
				if(count($ids)>0){
					$ids=array_keys($ids);
				}else{
					$ids=array(0);
				}
				if(!$isnogoods_ids){
					$params['id']=$ids;
				}
				$parameter=array(
					'params'=>$params,
					'limit'=>array($pindex,$psize),
				);
				$list=model('jy_weishop_en/goods')->goods_list($parameter,$total);
			//}
		}else{
			$parameter=array(
				'params'=>$params,
				'limit'=>array($pindex,$psize),
				'fields'=>$fields,
				'orderby'=>$orderby,
			);
			$list=model('jy_weishop_en/goods')->goods_list($parameter,$total);
		}
		//print_r($parameter);exit;
		if(empty($list)){
			return false;
		}else{
			$feemoneyset=$this->plugin_setting('feemoney');
			$feemoneyset['feerade']=$feemoneyset['feerade']?:5;
			foreach($list as $k=>&$itemv){
				$itemv['originalprice']=$itemv['originalprice']>0?$itemv['originalprice']:price($itemv['price']*1.2);
				$itemv['jianprice']=$itemv['originalprice']-$itemv['price'];
				if($feemoneyset['isopen']==1){
					$itemv['feemoney']=$itemv['marketprice']>0?$itemv['marketprice']-$itemv['price']:$feemoneyset['feerade']*$itemv['price']/100;
					$itemv['feemoney']=price($itemv['feemoney']);
				}
				$itemv['createtime']=$itemv['createtime']?:time()-58*3600*24;
				$itemv['createtime']=date('Y-m-d',$itemv['createtime']);
				//按点赞数排序作品
				$pindex = max(1, 0);
				$psize = 4;
				$members=pdo_getslice('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id']),array($pindex,$psize),$total,array('mid'),'','id desc');
				foreach($members as &$mv){
					$memberinfo=pdo_get('core_members',array('id'=>$mv['mid']),array('nickname','avatar'));
					$mv['nickname']=$memberinfo['nickname'];
					$mv['avatar']=$memberinfo['avatar'];
				}
				$itemv['members']=$members;			
				if($itemv['merchid'] && $this->plugin_exists('jy_merch')){
					$merch=pdo_get('jy_merch_user',array('id'=>$itemv['merchid']),array('status','merchname'));
					if($merch['status']==2 && $itemv['merchid']>0){
						pdo_update('jy_weishop_goods',array('nochecked'=>1),array('merchid'=>$itemv['merchid'],'uniacid'=>$_SESSION['uniacid']));
						unset($list[$k]);
						continue;
					}
					$itemv['merchname']=$merch['merchname'];
				}
				$itemv['likes']=pdo_count('jy_weishop_member_history',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id'],),'id');
				$cartinfo=pdo_get('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']));
				$cartinfo['total']=pdo_getsum('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
				$itemv['cartid']=$cartinfo['id']?:0;
				$itemv['carttotal']=$cartinfo['total']?:0;
				$itemv['cartprice']=$cartinfo['price']?:0;
				$itemv['cartoptionid']=$cartinfo['optionid']?:0;
				$itemv['cartid']=$itemv['cartid']?:0;
				$itemv['carttotal']=$itemv['carttotal']?:0;
				$itemv['specs_list']=array();
				if($_RQ['is_specs'] && $itemv['optionid']){
					//规格
					$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$itemv['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
					if($specs_list){
							foreach($specs_list as $k=>&$sp){
								$sp['items']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sp['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
							}
					}
					//已选规格
					$selectspecs = array();
					$selespename = '';
					$specs = pdo_getcolumn('jy_weishop_goods_option',array('id'=>$itemv['optionid'],'goodsid'=>$itemv['id']),'specs');				
					$selectspecs = str_array($specs,'_');
					if($selectspecs){
							foreach($selectspecs as $k=>&$spid){
								$title=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spid,'uniacid'=>$_SESSION['uniacid']),'title');
								$selespename .=$title.'，';
							}
					}
					//默认已选参数：
					$itemv['selespename'] = $selespename;
					$itemv['specs_list'] = $specs_list;
					$itemv['specid']=$selectspecs;
					$cartnum=pdo_getcolumn('jy_weishop_member_cart',array('selected'=>1,'optionid'=>$itemv['options']['id'],'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
					$itemv['post'][$itemv['options']['id']]=array('num'=>$cartnum?:0,'title'=>$selespename);
				}
				if($_SESSION['is_tomedia']){
					$itemv['image']=$itemv['image']?tomedia($itemv['image']):'';
					$itemv['video']=$itemv['video']?tomedia($itemv['video']):'';
				}
				if($_RQ['wxappjiaoyi']){
					$itemv['id']=$itemv['api_id'];
				}
				$itemv['api_data']=json_decode($itemv['api_data'],true);
			}
		}
		$list=array_values($list);
		return $list;
	}
}